package org.csu.chatserver.handlerDispatcher;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;

public class HeartbeatServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            if (e.state() == IdleState.WRITER_IDLE) {
                // 30s 未写：发协议层的 Ping，让浏览器自动回 Pong
                ctx.writeAndFlush(new PingWebSocketFrame());
            } else if (e.state() == IdleState.READER_IDLE) {
                // 60s 未读：认为客户端死链，主动 close
//                ctx.close();
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}

